<?php
declare (strict_types=1);

namespace app\controller\admin;

use app\common\controller\BaseController;
use app\model\admin\AvatarFrame as avatarFrameModel;
use think\App;
use \Throwable;

class AvatarFrame extends BaseController
{
    protected $model;

    public function __construct(App $app, avatarFrameModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 列表查询
     * @return array
     */
    public function query()
    {
        $where = [];
        $where['delete_time'] = 0;
        $where['status'] = 1;
        $page = input('page/d', 1);
        $pageSize = input('page_size/d', 20);
        $order = ['sort' => 'desc'];
        $fields = 'id,frame_type,title,frame_image,sort,status,price,create_time';
        return $this->model->getList($where, $fields, $order, $page, $pageSize);
    }

    /**
     * 新建
     * POST
     * @return array
     */
    public function create()
    {
        try {
            validate(\app\validate\admin\AvatarFrame::class)->scene('create')->check($this->params);
            return $this->model->add($this->params);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * 显示内容
     * @return array
     */
    public function detail()
    {
        $field = "id,frame_type,title,frame_image,sort,status,price,create_time";
        $info = $this->model->getOne($this->params, $field);
        return $this->returnRight($info);
    }

    /**
     * 编辑
     * PUT    blog/:id
     * @return array
     */
    public function update()
    {
        try {
            validate(\app\validate\admin\AvatarFrame::class)->scene('update')->check($this->params);
            $field = '*';
            return $this->model->edit($this->params, $field);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }


    /**
     * 删除指定资源
     * @return array
     */
    public function remove()
    {
        try {
            return $this->model->del($this->params);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * 启用
     * @return array
     */
    public function enable()
    {
        try {
            return $this->model->changeStatus($this->params, 1);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * 禁用
     * @return array
     */
    public function disable()
    {
        try {
            return $this->model->changeStatus($this->params, 0);
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }
}
